<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="650px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @deleteCancel="deleteCancel" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <div class="formTopFont">故障分类信息</div>
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item label="故障类型" prop="classifyName">
              <el-input v-model="ruleForm.classifyName" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="故障编码" prop="classifyCode">
              <el-input v-model="ruleForm.classifyCode" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-select v-model="ruleForm.status" style="width: 100%">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="停用"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="remark">
              <el-input v-model="ruleForm.remark" type="textarea" rows="3" maxlength="500" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
  </el-dialog>
</template>
<script>
export default {
  props: {
    deptAllList: {
      type: Array,
      default: () => {
        return []
      }
    },
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkPlanNo = async (rule, value, callback) => {
      const res = await this.$axios.get(
        `/equip/repair/faultClassify/verifyCodeUnique?classifyCode=${value}&id=${
          this.ruleForm.id || ''
        }`
      )
      if (res?.data.code === 200) {
        return res.data.data
          ? callback()
          : new Error(callback('故障分类编码重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      ruleForm: {
        id: '',
        classifyName: '',
        classifyCode: '',
        status: '',
        remark: ''
      },
      rules: {
        classifyName: [
          {
            required: true,
            message: '故障分类名称不能为空！',
            trigger: 'blur'
          }
        ],
        classifyCode: [
          {
            required: true,
            message: '故障分类编码不能为空！',
            trigger: 'blur'
          },
          { validator: checkPlanNo, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      obj: {}
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.obj = row
      this.dialogVisible = true
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    async deleteCancel() {
      if (this.title === '编辑') {
        this.$confirm(`确认删除数据？`, '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios
              .delete(`/equip/repair/faultClassify/remove?id=${this.obj.id}`)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$notify.success('删除成功！')
                  this.dialogVisible = false
                  this.$emit('getTableInfo', 1)
                } else {
                  this.$notify.error(res.data.msg)
                }
              })
          })
          .catch(() => {})
      }
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await this.$axios.post(
            '/equip/repair/faultClassify/submit',
            this.ruleForm
          )
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        id: '',
        classifyName: '',
        classifyCode: '',
        status: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
